package dac.sort.test;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;


public class ArrayGetter {
	
	public int[] getArrayFromFile(String fileName){
		int[] array = null;

		RandomAccessFile raf = null;
		try {
			raf = new RandomAccessFile(fileName, "r");
			FileChannel channel = raf.getChannel();

			String[] lines = null;
			ByteBuffer buffer = ByteBuffer.allocate((int)channel.size());
			channel.read(buffer);
			String fileData = new String(buffer.array(), "GBK");
			lines = fileData.split(",");

			ArrayList<Integer> list = new ArrayList<Integer>();
			for(String line : lines){
				String numberStr = line.trim();
				if(numberStr != null && numberStr.length()> 0){
					try {
						int number = Integer.parseInt(numberStr);
						list.add(number);
					} catch (NumberFormatException e) {
						e.printStackTrace();
					}
				}
			}
			array = new int[list.size()];
			for (int i = 0; i < list.size(); i++) {
				array[i] = list.get(i);
			}
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		} finally {
			try {
				if(raf != null) {
					raf.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return array;
	}

}
